package com.develope.tree.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.develope.tree.Image;
import com.develope.tree.login.User;

public class dbImage extends SQLiteOpenHelper {
	public static final String DATABASE_NAME = "MyDB.db";
	public static final String DATA_TABLE_NAME = "image";
	public static final int DATABASE_VERSION = 1;
	
	public static final String DATA_COLUMN_ID = "id";
	public static final String DATA_COLUMN_USER = "User";
	public static final String DATA_COLUMN_LO = "LO";
	public static final String DATA_COLUMN_CAY = "Cay";
	public static final String DATA_COLUMN_NGAY = "Ngay";
	public static final String DATA_COLUMN_PATH = "Path";
	
	public dbImage(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("create table image"
				+ "(id integer primary key, User text, Pass text)");
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS data");
		this.onCreate(db);
	}

	public boolean insertUser(Image image) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		
		contentValues.put("User", image.getUser());
		contentValues.put("Lo", image.getLo());
		contentValues.put("Cay", image.getCay());
		contentValues.put("Ngay", image.getNgay());
		contentValues.put("Path", image.getPath());
		db.insert("user", null, contentValues);
		return true;
	}
	
	public int numberOfRows() {
		SQLiteDatabase db = this.getReadableDatabase();
		int numRows = (int) DatabaseUtils.queryNumEntries(db, DATA_TABLE_NAME);
		return numRows;
	}
	
	public boolean updateImge(Integer id, Image image) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		
		contentValues.put("User", image.getUser());
		contentValues.put("Lo", image.getLo());
		contentValues.put("Cay", image.getCay());
		contentValues.put("Ngay", image.getNgay());
		contentValues.put("Path", image.getPath());
		db.insert("user", null, contentValues);
		
		db.update("user", contentValues, "id= ? ",
				new String[] { Integer.toString(id) });
		return true;
	}
	
	public Integer deleteImage(Integer id) {
		SQLiteDatabase db = this.getWritableDatabase();
		return db.delete("data", "id = ?",
				new String[] { Integer.toString(id) });
	}
	
}
